local SimplePowerBar = LibStub("AceAddon-3.0"):GetAddon("SimplePowerBar");
local Media = LibStub("LibSharedMedia-3.0")
local mediaWidgets = Media and LibStub("AceGUISharedMediaWidgets-1.0", true)

local FontStyle = {
	NONE = "None",
	OUTLINE = "Outline",
	THICKOUTLINE = "Thick Outline",
	["NONE, MONOCHROME"] = "No Outline, Monochrome",
	["OUTLINE, MONOCHROME"] = "Outline, Monochrome",
	["THICKOUTLINE, MONOCHROME"] = "Thick Outline, Monochrome"
}
local StatusBarOrientation = {
	HORIZONTAL = "Horizontal",
	VERTICAL = "Vertical"
}
local FullAlign = {TOPLEFT = "TOPLEFT",TOP = "TOP",TOPRIGHT = "TOPRIGHT",LEFT = "LEFT",CENTER = "CENTER",RIGHT = "RIGHT",BOTTOMLEFT = "BOTTOMLEFT",BOTTOM = "BOTTOM",BOTTOMRIGHT = "BOTTOMRIGHT"}
local AlignH = {LEFT = "LEFT", CENTER = "CENTER", RIGHT = "RIGHT"}
local AlignV = {BOTTOM = "BOTTOM", CENTER = "CENTER", TOP = "TOP"}
local FrameStratas = {BACKGROUND = "Background",LOW = "Low",MEDIUM = "Medium",HIGH = "High",DIALOG = "Dialog",FULLSCREEN_DIALOG = "Fullscreen Dialog",TOOLTIP = "Tooltip"}

local function GetValue(info)
	if type(info.arg) == "table" then
		local arg1, arg2 = info.arg[1], info.arg[2]
		return SimplePowerBar.db.profile[arg1][arg2]
	else
		return SimplePowerBar.db.profile[info.arg]
	end
end

local function SetValue(info,v)
	if type(info.arg) == "table" then
		local arg1, arg2 = info.arg[1], info.arg[2]
		SimplePowerBar.db.profile[arg1][arg2] = v
	else
		SimplePowerBar.db.profile[info.arg] = v
	end
	SimplePowerBar:FullUpdate()
end

local function GetColor(info)
	local color = SimplePowerBar.db.profile[info.arg]
	return color.r, color.g, color.b, (color.a or 1)
end

local function SetColor(info,r,g,b,a)
	local color = SimplePowerBar.db.profile[info.arg]
	color.r, color.g, color.b, color.a = r,g,b,a
	SimplePowerBar:FullUpdate()
end

local options = nil;
local function GetOptions()
	local db = SimplePowerBar.db.profile
	if not options then
		options = {
			name = GetAddOnMetadata("SimplePowerBar", "title").." v"..GetAddOnMetadata("SimplePowerBar", "version"),
			handler = SimplePowerBar,
			type = "group",
			childGroups = "tab",
			get = GetValue,
			set = SetValue,
			args = {
				Textures = {
					name = "Textures",
					type = "group",
					order = 10,
					args = {
						Statusbar = {
							name = "Statusbar",
							type = "group",
							inline = true,
							order = 1,
							args = {
								Texture = { 
									name = "Texture",
									type = "select",
									order = 1,							
									dialogControl = mediaWidgets and "LSM30_Statusbar" or nil,
									values = Media:HashTable("statusbar"),
									arg = "StatusTex",
								},
								Orientation = {
									name = "Orientation",
									type = "select",
									order = 1.5,
									values = StatusBarOrientation,
									arg = "Orientation",
								},
								CustomColor = {
									name = "Coloring Method",
									type = "group",
									order = 2,
									inline = true,
									args = {
										ColorMethod = {
											name = "Use Color Fade",
											desc = "Transitions the color from green to yellow to red depending on the current value of alternate power.",
											descStyle = "inline",
											type = "toggle",
											order = 1,
											width = "full",
											arg = "fade",
										},
										Header1 = {
											name = "",
											type = "header",											
											order = 2,
										},
										Desc1 = {
											name = "Disabling the color fade will allow you to set a custom color of your choice.",
											type = "description",
											order = 3,
										},
										Color = {
											name = "Custom Color",
											type = "color",
											order = 4,
											disabled = function() if db.fade then return true else return false end end,
											get = GetColor,
											set = SetColor,
											arg = "StatusColor",
										},
									},
								},
							},
						},
						BG = {
							name = "Background",
							type = "group",
							inline = true,
							order = 2,
							args = {
								Color = {
									name = "Color",
									type = "color",
									order = 3,									
									get = GetColor,
									set = SetColor,
									hasAlpha = true,
									arg = "BackgroundColor",
								},
							},
						},
						Border = {
							name = "Border",
							type = "group",
							inline = true,
							order = 3,
							args = {
								--BorderSize = {
									--name = "Border Size",
									--type = "range",
									--order = 1,
									--min = 0.1,
									--max = 16,
									--width = "full",
									--step = 0.1,
									--arg = "borderSize",
								--},
								--BorderInset = {
									--name = "Border Inset",
									--type = "range",
									--order = 2,
									--min = -16,
									--max = 16,
									--width = "full",
									--step = 0.1,
									--arg = "borderInset",
								--},
								Color = {
									name = "Color",
									type = "color",
									order = 3,									
									get = GetColor,
									set = SetColor,
									hasAlpha = true,
									arg = "BorderColor",
								},
							},
						},
					},
				},
				PowerText = {
					name = "Power Text",
					type = "group",
					order = 20,
					args = {
						Toggles = {
							name = "Toggles",
							type = "group",
							inline = true,
							order = 1,
							args = {
								Amount = {
									name = "Enable Text",
									type = "toggle",
									order = 1,
									arg = "amount",
								},
								Header1 = {
									name = "",
									type = "header",
									order = 2,
								},
								Level = {
									name = "Show Power Level",
									type = "toggle",
									order = 3,
									arg = "level",									
								},
								Percent = {
									name = "Show Percent",
									type = "toggle",
									order = 4,
									arg = "percent",
								},
							},
						},
						Text = {
							name = "Font Settings",
							type = "group",
							inline = true,
							order = 2,
							disabled = function() if db.amount then return false else return true end end,
							args = {
								Font = {
									name = "Font",
									type = "select",
									order = 1,
									dialogControl = "LSM30_Font",
									values = Media:HashTable("font"),
									get = GetValue,
									set = SetValue,
									arg = {"StatusBarText","font"},
								},
								Header0 = {
									name = "",
									type = "header",
									order = 1.5
								},
								Size = {
									name = "Size",
									type = "range",
									order = 2,									
									min = 4,
									max = 24,
									step = 1,
									arg = {"StatusBarText","size"},
								},
								Style = {
									name = "Style",
									type = "select",
									order = 3,
									values = FontStyle,
									arg = {"StatusBarText","style"},
								},
								Header1 = {
									name = "Text Shadow",
									type = "header",
									order = 4,									
								},
								ShadowEnable = {
									name = "Show Shadow",
									type = "toggle",
									order = 4.25,
									get = function(info) 
											local arg1, arg2 = info.arg[1], info.arg[2]
											if SimplePowerBar.db.profile[arg1][arg2] == 1 then
												return true
											else
												return false
											end
										end,
									set = function(info,v)
											local arg1, arg2 = info.arg[1], info.arg[2]
											if v == true then
												SimplePowerBar.db.profile[arg1][arg2] = 1
											else
												SimplePowerBar.db.profile[arg1][arg2] = 0
											end
											SimplePowerBarText:SetShadowColor(0,0,0,SimplePowerBar.db.profile[arg1][arg2])
										end,
									arg = {"StatusBarText","shadow"},
								},
								ShadowDistance = {
									name = "Shadow Offset",
									type = "range",
									order = 4.5,
									min = -2,
									max = 2,
									step = 0.01,
									arg = {"StatusBarText","shadowOffset"},
								},
								Header2 = {
									name = "Placement",
									type = "header",
									order = 4.75,
								},
								Alignment = {
									name = "Alignment",
									type = "select",
									
									order = 5,
									values = FullAlign,
									arg = {"StatusBarText","align"},
								},
								Anchor = {
									name = "Anchor",
									type = "select",
									order = 6,
									values = FullAlign,
									arg = {"StatusBarText","anchor"},
								},
								Header3 = {
									name = "",
									type = "header",
									order = 7,
								},
								ofx = {
									name = "Offset X",
									type = "range",
									order = 8,
									min = -120,
									max = 120,
									step = 1,
									arg = {"StatusBarText","ofx"},
								},
								ofy = {
									name = "Offset Y",
									type = "range",
									order = 9,
									min = -120,
									max = 120,
									step = 1,
									arg = {"StatusBarText","ofy"},
								},
							},
						},						
					},
				},
				LabelText = {
					name = "Label Text",
					type = "group",
					order = 30,
					args = {
						Toggles = {
							name = "Toggles",
							type = "group",
							inline = true,
							order = 1,
							args = {
								Label = {
									name = "Show Label",
									type = "toggle",
									order = 1,
									width = "full",
									arg = "label",
								},
							},
						},
						Text = {
							name = "Font Settings",
							type = "group",
							inline = true,
							order = 2,
							disabled = function() if db.label then return false else return true end end,
							args = {
								Font = {
									name = "Font",
									type = "select",
									order = 1,
									dialogControl = "LSM30_Font",
									values = Media:HashTable("font"),
									get = GetValue,
									set = SetValue,
									arg = {"LabelText","font"},
								},
								Header0 = {
									name = "",
									type = "header",
									order = 1.5
								},
								Size = {
									name = "Size",
									type = "range",
									order = 2,									
									min = 4,
									max = 24,
									step = 1,
									arg = {"LabelText","size"},
								},
								Style = {
									name = "Style",
									type = "select",
									order = 3,
									values = FontStyle,
									arg = {"LabelText","style"},
								},
								Header1 = {
									name = "Text Shadow",
									type = "header",
									order = 4,									
								},
								ShadowEnable = {
									name = "Show Shadow",
									type = "toggle",
									order = 4.25,
									get = function(info) 
											local arg1, arg2 = info.arg[1], info.arg[2]
											if SimplePowerBar.db.profile[arg1][arg2] == 1 then
												return true
											else
												return false
											end
										end,
									set = function(info,v)
											local arg1, arg2 = info.arg[1], info.arg[2]
											if v == true then
												SimplePowerBar.db.profile[arg1][arg2] = 1
											else
												SimplePowerBar.db.profile[arg1][arg2] = 0
											end
											SimplePowerBarLabel:SetShadowColor(0,0,0,SimplePowerBar.db.profile[arg1][arg2])
										end,
									arg = {"LabelText","shadow"},
								},
								ShadowDistance = {
									name = "Shadow Offset",
									type = "range",
									order = 4.5,
									min = -2,
									max = 2,
									step = 0.01,
									arg = {"LabelText","shadowOffset"},
								},
								Header2 = {
									name = "Placement",
									type = "header",
									order = 4.75,
								},
								Alignment = {
									name = "Alignment",
									type = "select",
									
									order = 5,
									values = FullAlign,
									arg = {"LabelText","align"},
								},
								Anchor = {
									name = "Anchor",
									type = "select",
									order = 6,
									values = FullAlign,
									arg = {"LabelText","anchor"},
								},
								Header3 = {
									name = "",
									type = "header",
									order = 7,
								},
								ofx = {
									name = "Offset X",
									type = "range",
									order = 8,
									min = -120,
									max = 120,
									step = 1,
									arg = {"LabelText","ofx"},
								},
								ofy = {
									name = "Offset Y",
									type = "range",
									order = 9,
									min = -120,
									max = 120,
									step = 1,
									arg = {"LabelText","ofy"},
								},
							},
						},
					},
				},
				SizePlacement = {
					name = "Size & Placement",
					type = "group",
					order = 40,
					args = {
						Size = {
							name = "Sizing",
							type = "group",
							inline = true,
							order = 1,
							args = {
								Width = {
									name = "Width",
									type = "range",
									order = 1,
									min = 10,
									max = 1200,
									step = 1,
									arg = "width",
								},
								Height = {
									name = "Height",
									type = "range",
									order = 2,
									min = 10,
									max = 1200,
									step = 1,
									arg = "height",
								},
							},
						},
						Placement = {
							name = "Placement",
							type = "group",
							inline = true,
							order = 2,
							args = {
								Lock = {
									name = "Lock Position",
									type = "toggle",
									order = 1,
									arg = "locked",
								},
								Header1 = {
									name = "",
									type = "header",
									order = 2,
								},
								x = {
									name = "X",
									type = "range",
									order = 3,
									min = -1200,
									max = 1200,
									step = 1,
									arg = "x",
								},
								y = {
									name = "Y",
									type = "range",
									order = 4,
									min = -1200,
									max = 1200,
									step = 1,
									arg = "y",
								},
								Header2 = {
									name = "",
									order = 5,
									type = "header",
								},
								FrameStrata = {
									name = "Frame Strata",
									order = 6,
									type = "select",
									values = FrameStratas,
									arg = "frameStrata",
								},
								FrameLevel = {
									name = "Frame Level",
									order = 7,
									type = "range",
									min = 1,
									max = 32,
									step = 1,									
									arg = "frameLevel",
								},
							},
						},
					},
				},
			},
		}
	end
	return options
end

local intoptions = nil;
local function GetIntOptions()
	if not intoptions then
		intoptions = {
			name = GetAddOnMetadata("SimplePowerBar", "title").." v"..GetAddOnMetadata("SimplePowerBar", "version"),
			handler = SimplePowerBar,
			type = "group",
			args = {
				note = {
					type = "description",
					name = "You can access the "..GetAddOnMetadata("SimplePowerBar", "title").." v"..GetAddOnMetadata("SimplePowerBar", "version").." options by typing: /spb",
					order = 10,
				},
				openoptions = {
					type = "execute",
					name = "Open Config",
					width = "full",
					func = function() 
						SimplePowerBar:OpenOptions()
					end,
					order = 20,
				},
			},
		};
	end
	return intoptions;
end

function SimplePowerBar:OpenOptions()
	HideUIPanel(InterfaceOptionsFrame)
	HideUIPanel(GameMenuFrame)
	if not options then SimplePowerBar:SetUpOptions() end
	LibStub("AceConfigDialog-3.0"):Open("SimplePowerBar");
end

function SimplePowerBar:ChatCommand(input)
	SimplePowerBar:OpenOptions();
end

function SimplePowerBar:ConfigRefresh()
	local db = self.db.profile;
	Update()
end

function SimplePowerBar:SetUpInitialOptions()
	-- Chat Command
	--self:RegisterChatCommand("/spb", "ChatCommand");
	
	-- Interface panel options
	
	LibStub("AceConfig-3.0"):RegisterOptionsTable("SimplePowerBar Dialog", GetIntOptions);
	
	self.optionsFrame = LibStub("AceConfigDialog-3.0"):AddToBlizOptions("SimplePowerBar Dialog", "SimplePowerBar");
end

function SimplePowerBar:SetUpOptions()
	local db = self.db.profile;
	
	-- Options Window
	GetOptions();
	
	options.args.profiles = LibStub("AceDBOptions-3.0"):GetOptionsTable(self.db);
	options.args.profiles.order = 10000;
	
	LibStub("AceConfig-3.0"):RegisterOptionsTable("SimplePowerBar", options);
	LibStub("AceConfigDialog-3.0"):SetDefaultSize("SimplePowerBar", 750, 450)
end